package com.diankangwo.factory.controller;

import com.diankangwo.common.annotation.SysLog;
import com.diankangwo.common.utils.R;
import com.diankangwo.factory.entity.TdOnlinepaylog;
import com.diankangwo.factory.entity.TdOrder;
import com.diankangwo.factory.entity.TdOrderShoppingcart;
import com.diankangwo.factory.entity.TdShoppingcart;
import com.diankangwo.factory.service.*;
import com.diankangwo.util.PayStatus;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台首页数据Controller
 */
@RestController
@RequestMapping("/fac/homepage")
public class TdHomePageController {

    @Autowired
    private TdUserInfoService tdUserInfoService;

    @Autowired
    private TdOrderService tdOrderService;

    @Autowired
    private TdOnlinePayLogService tdOnlinePayLogService;

    @Autowired
    private TdDiscussServices tdDiscussServices;

    @Autowired
    private TdCollectionServices tdCollectionServices;

    @Autowired
    private TdOrderShoppingcartServices tdOrderShoppingcartServices;

    @Autowired
    private TdShoppingcartServices tdShoppingcartServices;

    /**
     * 查询后台首页数据
     */
    @ApiOperation(value = "查询后台首页数据", notes = "")
    @ApiImplicitParam(name = "userinfoId", value = "会员id", required = true, dataType = "Integer")
    @SysLog("查询后台首页数据")
    @RequestMapping("/findData")
    @RequiresPermissions("fac:homepage:findData")
    public R findData() {

        //查询今日销售总额
        int totalMoney = 0;
        LocalDate date = LocalDate.now();

        List<TdOnlinepaylog> tdOnlinepaylogs = tdOnlinePayLogService.queryByDay(date.toString() + " 00:00:00", date.toString() + " 23:59:59");
        for (int i = 0; i < tdOnlinepaylogs.size(); i++) {
            totalMoney += tdOnlinepaylogs.get(i).getTotalfee();
        }

        //查询今日订单总数
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", 1);
        map.put("limit", Integer.MAX_VALUE);
        int orderTotal = tdOrderService.queryByDay(date.toString() + " 00:00:00", date.toString() + " 23:59:59").size();

        //查询今日评论数
        int discussTotal = tdDiscussServices.queryByDay(date.toString() + " 00:00:00", date.toString() + " 23:59:59").size();

        //查询今日收藏数
        int collectionTotal = tdCollectionServices.queryByDay(date.toString() + " 00:00:00", date.toString() + " 23:59:59").size();

        //查询用户信息统计（今日新增、昨日新增、本月新增、会员总数）
        int userinfoDayTotal = tdUserInfoService.queryByDay(date.toString() + " 00:00:00", date.toString() + " 23:59:59").size();
        String year = date.getYear() + "-";
        String month = date.getMonthValue() + "-";
        String day = date.getDayOfMonth() - 1 + "";
        int userinfoYesterdayTotal = tdUserInfoService.queryByDay(year + month + day + " 00:00:00", year + month + day + " 23:59:59").size();
        month = date.getMonthValue() - 1 + "-";
        day = date.getDayOfMonth() + "";
        int userinfoMonthTotal = tdUserInfoService.queryByDay(year + month + day + " 00:00:00", year + month + day + " 23:59:59").size();
        int userinfoTotal = tdUserInfoService.queryTotal(map);
        //按状态查询订单（未确认订单、待支付订单、待发货订单、已成交订单、用户退款申请、退款完成）
        int unpaid = tdOrderService.queryByStatus(PayStatus.unpaid.getName().toString()).size();
        int beforedeliver = tdOrderService.queryByStatus(PayStatus.beforedeliver.getName().toString()).size();
        int delivering = tdOrderService.queryByStatus(PayStatus.delivering.getName().toString()).size();
        int success = tdOrderService.queryByStatus(PayStatus.success.getName().toString()).size();
        int refund = tdOrderService.queryByStatus(PayStatus.refund.getName().toString()).size();
        int refundsuccess = tdOrderService.queryByStatus(PayStatus.refundsuccess.getName().toString()).size();
        int uncommented = tdOrderService.queryByStatus(PayStatus.uncommented.getName().toString()).size();
        //订单统计、销售统计（按周查询）
        year = date.getYear() + "-";
        month = date.getMonthValue() + "-";
        int[] orderTotalNum = new int[7];
        int[] sellTotalNum = new int[7];
        for (int i = 0; i < 7; i++) {
            day = date.getDayOfMonth() - i + "";
            List<TdOrder> tdOrders = tdOrderService.queryByDay(year + month + day + " 00:00:00", year + month + day + " 23:59:59");
            orderTotalNum[i] = tdOrders.size();
            int num = 0;
            for (int j = 0; j < tdOrders.size(); j++) {
                List<TdOrderShoppingcart> tdOrderShoppingcart = tdOrderShoppingcartServices.selectByOrderId(tdOrders.get(j).getId());
                for (int k = 0; k < tdOrderShoppingcart.size(); k++) {
                    TdShoppingcart tdShoppingcart = tdShoppingcartServices.selectByPrimaryKey(tdOrderShoppingcart.get(k).getShoppingcartId());
                    num += tdShoppingcart.getCommoditynumber();
                }
                sellTotalNum[i] = num;
            }
        }
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("totalMoney", totalMoney);
        result.put("orderTotal", orderTotal);
        result.put("discussTotal", discussTotal);
        result.put("collectionTotal", collectionTotal);
        result.put("userinfoDayTotal", userinfoDayTotal);
        result.put("userinfoYesterdayTotal", userinfoYesterdayTotal);
        result.put("userinfoTotal", userinfoTotal);
        result.put("userinfoMonthTotal", userinfoMonthTotal);
        result.put("uncommented", uncommented);
        result.put("unpaid", unpaid);
        result.put("success", success);
        result.put("beforedeliver", beforedeliver);
        result.put("delivering", delivering);
        result.put("refund", refund);
        result.put("refundsuccess", refundsuccess);
        result.put("orderTotalNum", orderTotalNum);
        result.put("sellTotalNum", sellTotalNum);

        return R.ok().put("result", result);
    }

}
